package com.sky.wsp.drools.controller;

import com.sky.wsp.drools.entity.InsuranceInfo;
import com.sky.wsp.drools.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;


    @RequestMapping("/checkRule")
    public Map checkRule() {
        InsuranceInfo insuranceInfo = getInsuranceInfo();
        List<String> list = ruleService.rule(insuranceInfo);
        return returnResult(list);
    }

    @RequestMapping("/insuranceInfoCheck")
    public Map insuranceInfoCheck() {

        InsuranceInfo insuranceInfo = getInsuranceInfo();
        List<String> list = null;
        try {
            list = ruleService.insuranceInfoCheck(insuranceInfo);
        } catch (Exception e) {
            e.printStackTrace();
            Map map = new HashMap();
            map.put("checkResult", false);
            map.put("msg", "未知错误");
            return map;
        }
        return returnResult(list);
    }

    private InsuranceInfo getInsuranceInfo() {
        //模拟数据，实际应为页面传递过来
        InsuranceInfo insuranceInfo = new InsuranceInfo();
        insuranceInfo.setParam1("picc");
        insuranceInfo.setParam4("上海");
        insuranceInfo.setParam5("101");
        insuranceInfo.setParam6("12");
        insuranceInfo.setParam7("222");
        insuranceInfo.setParam8("1");
        insuranceInfo.setParam13("3");
        return insuranceInfo;
    }

    private Map returnResult(List<String> list) {
        Map map = new HashMap();
        if (list != null && list.size() > 0) {
            map.put("checkResult", false);
            map.put("msg", "准入失败");
            map.put("detail", list);
        } else {
            map.put("checkResult", true);
            map.put("msg", "准入成功");
        }
        return map;
    }
}